package com.xxcxcxcx.jobsystem.common.Job.Miner;

import com.xxcxcxcx.jobsystem.PlayerSaveData;
import com.xxcxcxcx.jobsystem.common.Job.JobBase;
import com.xxcxcxcx.jobsystem.common.JobsClass;
import com.xxcxcxcx.jobsystem.common.capability.JobCapability;
import com.xxcxcxcx.jobsystem.common.capability.JobCapabilityProvider;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.material.Material;
import net.minecraftforge.common.util.LazyOptional;

import javax.annotation.Nullable;

import static net.minecraft.world.effect.MobEffects.DIG_SPEED;

public class Miner extends JobBase {
    public static String Job = JobsClass.Miner;
    public static float GetMineSpeed(Player player , BlockState blockstate , float oldspeed){
        float newspeed=oldspeed;
        if(GetRightBlock(blockstate)){
            newspeed *=GetUP(player);
        }
        return newspeed;
    }
    public static void GetBreak(BlockState blockState, Player player){
        LazyOptional<JobCapability> capability = JobCapabilityProvider.getPlayerData(player);
        if(!capability.isPresent()) {
            return;
        }
        PlayerSaveData playerSaveData = capability.orElse(null);
        int Joblevel = playerSaveData.getLevel(Job);
        if(Joblevel<2&&GetStone(blockState))UpExperience(player,2,Job);
        int num=10;
        if(Joblevel>4)num-=5;
        if(Joblevel>7)num-=3;
        if(GetOre(blockState))UpExperience(player,num,Job);
        if(blockState.getBlock()==Blocks.DIAMOND_ORE||blockState.getBlock()==Blocks.DEEPSLATE_DIAMOND_ORE)UpExperience(player,15,Job);
    }
    public static Block[] stones={Blocks.STONE,Blocks.GRANITE,Blocks.DIORITE,Blocks.ANDESITE};
    public static Block[] ore={Blocks.COAL_ORE,Blocks.DEEPSLATE_COAL_ORE,Blocks.IRON_ORE,Blocks.DEEPSLATE_IRON_ORE,
                                Blocks.COPPER_ORE,Blocks.DEEPSLATE_COPPER_ORE,Blocks.LAPIS_ORE,Blocks.DEEPSLATE_LAPIS_ORE,
                                Blocks.GOLD_ORE,Blocks.DEEPSLATE_GOLD_ORE,Blocks.REDSTONE_ORE,Blocks.DEEPSLATE_REDSTONE_ORE,
                                Blocks.DIAMOND_ORE,Blocks.DEEPSLATE_DIAMOND_ORE,Blocks.EMERALD_ORE,Blocks.NETHER_QUARTZ_ORE,
                                Blocks.NETHER_GOLD_ORE,Blocks.ANCIENT_DEBRIS};

    public static Item[] simple = {Items.RAW_IRON,Items.RAW_GOLD,Items.RAW_COPPER,Items.COAL,Items.REDSTONE,Items.LAPIS_LAZULI};
    public static Item[] high = {Items.RAW_IRON_BLOCK,Items.RAW_GOLD_BLOCK,Items.RAW_COPPER_BLOCK,Items.DIAMOND,Items.COAL_BLOCK};

    public static void DropMore(BlockState blockState, Level level, BlockPos blockPos, @Nullable BlockEntity blockEntity, Player player, ItemStack itemStack) {
        if (level instanceof ServerLevel serverLevel) {
            LazyOptional<JobCapability> capability = JobCapabilityProvider.getPlayerData(player);
            if(!capability.isPresent()) {
                return;
            }
            PlayerSaveData playerSaveData = capability.orElse(null);
            int joblevel = playerSaveData.getLevel(Job);
            if(joblevel>=7&&player.getRandom().nextInt(20-joblevel)==1)player.addEffect(new MobEffectInstance(DIG_SPEED,100,1));
            if(joblevel==10&&player.getRandom().nextInt(20-joblevel)==1)player.addEffect(new MobEffectInstance(DIG_SPEED,100,2));
            if(GetOre(blockState)){
                if(joblevel>=4 && player.getRandom().nextDouble() < (joblevel*0.03)){
                    Block.getDrops(blockState, (ServerLevel)level, blockPos, blockEntity, player, itemStack).forEach((itemStack1) -> {
                        for(Item i : simple){
                            if(i==itemStack1.getItem()){
                                itemStack1.setCount(player.getRandom().nextInt(joblevel-3));
                                Block.popResource(level, blockPos, itemStack1);
                            }
                        }
                        if(joblevel>=7){
                            for(Item i : high){
                                if(i==itemStack1.getItem()){
                                    itemStack1.setCount(player.getRandom().nextInt(joblevel-6));
                                    Block.popResource(level, blockPos, itemStack1);
                                }
                            }
                        }
                    });
                }
                if(joblevel==10&& player.getRandom().nextDouble() < (joblevel*0.01)){
                    Item i=high[player.getRandom().nextInt(high.length)];
                    ItemStack itemStack1 = new ItemStack(i,player.getRandom().nextInt(1,joblevel-6));
                    Block.popResource(level, blockPos, itemStack1);
                }
            }
            if(GetStone(blockState)){
                if(joblevel>=7&&player.getRandom().nextDouble() < (joblevel*0.004)) {
                    Item i=simple[player.getRandom().nextInt(simple.length)];
                    ItemStack itemStack1 = new ItemStack(i,player.getRandom().nextInt(1,joblevel-5));
                    Block.popResource(level, blockPos, itemStack1);
                }
                if(joblevel>=7&&player.getRandom().nextDouble() < (joblevel*0.002)) {
                    Item i=high[player.getRandom().nextInt(high.length)];
                    ItemStack itemStack1 = new ItemStack(i,player.getRandom().nextInt(1,joblevel-5));
                    Block.popResource(level, blockPos, itemStack1);
                }
            }
        }
    }
    public static boolean GetRightBlock(BlockState block){
        return GetOre(block) || GetStone(block);
    }
    public static boolean GetStone(BlockState block){
        return block.getMaterial()== Material.STONE;
    }
    public static boolean GetOre(BlockState block){
        for (Block block1:ore){
            if(block.getBlock()==block1)return true;
        }
        return false;
    }
    private static double GetUP(Player player){
        LazyOptional<JobCapability> capability = JobCapabilityProvider.getPlayerData(player);
        if(!capability.isPresent()) {
            return 1;
        }
        PlayerSaveData playerSaveData = capability.orElse(null);
        int level =playerSaveData.getLevel(Job);
        double num=1.0d;
        if(level<4){
            num *= (level+1)*0.20;
        }
        if(level>=4){
            num *= (1+(level - 4) * 0.2);
        }
        if(level==10&&player.getRandom().nextInt(10)==0){
            num *= 10;
        }
        return num;
    }
}
